﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;

namespace NutriLeaf.Phone.Core.ModuleVersion
{
    public class ModuleVersion : INotifyPropertyChanged
    {
        ModuleVersionService.ModuleVersionClient client;
        public string ModuleName { get; set; }
        public int VersionNumber { get; set; }
        public int ModuleID { get; set; }
        public bool IsLatest { get; set; }

        public ModuleVersion(string name)
        {
            client = new ModuleVersionService.ModuleVersionClient();
            Model.ModuleVersion module = GetModule(name);
            if (module == null)
            {
                IsLatest = false;
                NotifyPropertyChanged("IsLatest");
            }
            else
            {
                ModuleName = module.VersionName;
                VersionNumber = module.VersionNumber;
                ModuleID = module.ModuleVersionID;
                IsLatestVersion();
            }
        }



        public Model.ModuleVersion GetModule(string name)
        {
            Model.NutriLeafDataContext context = Model.Context.GetContext();

            Model.ModuleVersion version = (from Model.ModuleVersion ver
                                           in context.ModuleVersions
                                           where ver.VersionName == name
                                           select ver).SingleOrDefault();

            return version;

        }

        public void IsLatestVersion()
        {
            
                client.IsLatestCompleted += new EventHandler<ModuleVersionService.IsLatestCompletedEventArgs>(client_IsLatestCompleted);
                client.IsLatestAsync(this.ModuleName, this.VersionNumber);
            
        }

        public void UpdateModule()
        {
            client.GetModuleCompleted += new EventHandler<ModuleVersionService.GetModuleCompletedEventArgs>(client_GetModuleCompleted);
            client.GetModuleAsync(this.ModuleName);
        }

        void client_GetModuleCompleted(object sender, ModuleVersionService.GetModuleCompletedEventArgs e)
        {
            
        }

        

        void client_IsLatestCompleted(object sender, ModuleVersionService.IsLatestCompletedEventArgs e)
        {
            this.IsLatest = e.Result;
            NotifyPropertyChanged("IsLatest");
        }


        private void NotifyPropertyChanged(String name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
